﻿using System;
using System.Linq;
using System.Windows.Forms;
using OwnZTec.LojaDelivery.Data;

namespace OwnZTec.LojaDelivery.UI.WinForms
{
    public partial class FrmDesperdicio : Form
    {
        #region -- Membros e Construtores -------------------------------------------------------------------------------------------------
        public FrmDesperdicio()
        {
            InitializeComponent();
            this.ConfigurarFormFilho();
        }
        private void FrmDesperdicio_Load(object sender, EventArgs e)
        {
            cmdEditar.Enabled = cmdExcluir.Enabled = false;

            AtualizarGrid();

            Contexto.MainForm.Cursor = Cursors.Arrow;
        }

        #endregion ------------------------------------------------------------------------------------------------------------------------

        #region -- Eventos ----------------------------------------------------------------------------------------------------------------
        private void cmdSair_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void cmdAtualizar_Click(object sender, EventArgs e)
        {
            AtualizarGrid();
        }
        private void cmdAdicionar_Click(object sender, EventArgs e)
        {
            var frm = new FrmDesperdicioDetalhes
            {
                Pai = this
            };
            frm.ShowDialog();
        }
        private void gvDesperdicio_DoubleClick(object sender, EventArgs e)
        {
            if (gvDesperdicio.SelectedRows.Count != 1)
                return;

            var frm = new FrmDesperdicioDetalhes()
            {
                Desperdicio = (tbDesperdicio)gvDesperdicio.SelectedRows[0].DataBoundItem,
                Edicao = true,
                Pai = this
            };

            frm.ShowDialog();
        }
        private void cmdEditar_Click(object sender, EventArgs e)
        {
            var frm = new FrmDesperdicioDetalhes()
            {
                Desperdicio = (tbDesperdicio)gvDesperdicio.SelectedRows[0].DataBoundItem,
                Edicao = true,
                Pai = this
            };

            frm.ShowDialog();
        }

        private void gvDesperdicio_SelectionChanged(object sender, EventArgs e)
        {
            if (gvDesperdicio.SelectedRows.Count == 0)
                cmdEditar.Enabled = cmdExcluir.Enabled = false;
            else if (gvDesperdicio.SelectedRows.Count == 1)
            {
                cmdEditar.Enabled = cmdExcluir.Enabled = true;
            }
            else
            {
                cmdEditar.Enabled = false;
                cmdExcluir.Enabled = true;
            }
        }

        private void cmdExcluir_Click(object sender, EventArgs e)
        {
            var res = Contexto.MainForm.ExibirMensagem("Deseja excluir o registro selecionado?",
                                           MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (res == DialogResult.No)
                return;

            var pd = ((tbDesperdicio)gvDesperdicio.SelectedRows[0].DataBoundItem);

            int index = pd.idDesperdicio;

            foreach (OwnZTec.LojaDelivery.Data.tbDesperdicioDetalhe item in Contexto.Db.tbDesperdicioDetalhes.Where(x => x.idDesperdicio == index))
            {
                Contexto.Db.tbDesperdicioDetalhes.DeleteObject(item);
            }

            Contexto.Db.SaveChanges();

            Contexto.Db.tbDesperdicios.DeleteObject(pd);

            Contexto.Db.SaveChanges();

            AtualizarGrid();
        }

        #endregion ------------------------------------------------------------------------------------------------------------------------
        #region -- Metodos ----------------------------------------------------------------------------------------------------------------
        public void AtualizarGrid()
        {
            Cursor = Cursors.WaitCursor;
            gvDesperdicio.DataSource = Contexto.Db.tbDesperdicios.ToList();
            Cursor = Cursors.Arrow;
        }
        #endregion ------------------------------------------------------------------------------------------------------------------------

        
    }
}
